from django.shortcuts import render
from rest_framework import status
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.decorators import api_view
from story.serializers import StorySerializer
from story.models import Story
from story.models import StoryLine
import logging

# Create your views here.
@api_view(['POST', 'GET'])
def story(request):
    storyId = request.QUERY_PARAMS.get('storyId', None)
    data = request.DATA

    if request.method == 'GET':
        if storyId is not None:
            try:
                retreivedStory = Story.objects.get(storyId = storyId)
                storySerializer = StorySerializer(retreivedStory)
            except Story.DoesNotExist:
                return Response("invalid story id", status.HTTP_400_BAD_REQUEST)

            return Response(storySerializer.data)
        else:
            return Response("story id is empty", status.HTTP_400_BAD_REQUEST)
    elif request.method == 'POST':
        storySerializer = StorySerializer(data=data)
        if storySerializer.is_valid():
            storySerializer.save()
            return Response(storySerializer.data, status.HTTP_200_OK)
        return Response(storySerializer.errors, status.HTTP_400_BAD_REQUEST)

